# This file is part of Howlr.
#
# Howlr is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Howlr is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Howlr.  If not, see <http://www.gnu.org/licenses/>.

# need auto_validation off to render non-XHTML XML
Markaby::Builder.set(:auto_validation, false)
Markaby::Builder.set(:indent, 2)

module Howlr::Views
  module XML
    def message
      @message.to_xml(:indent => 2)
    end
  end
  
  module HTML
    def message
      html do
        h1 "Message #{@message.id}"
          
          dl do
            dt {'id'}
            dd @message.id
            
            dt {'recipient(s)'} 
            dd @message.recipients.collect{|r| r.to_s}.join(", ")
            
            dt {'from'}
            dd {@message.from}
            
            dt {'subject'}
            dd {@message.subject}
            
            dt {'body'}
            dd {@message.body}
            
            dt {'sent successfully?'}
            dd {@message.sent}
            
            unless @message.delivery_errors.empty?
              dt {'delivery errors'}
              dd {@message.delivery_errors.join("<br/>")}
            end
          end
      end
    end
    
    def message_list
      html do
        h1 "Messages Sent"
        dl do
          dt 'Number of messages sent out since last server reboot:'
          dd @messages.size
        end
        table do
          thead do
            th 'id'
            th 'subject'
            th 'to'
            th 'from'
            th 'sent successfully?'
          end
          @messages.each do |m|
            tr(:id => "message-#{m.id}") do
              td {a(:href => R(Howlr::Controllers::Messages, m.id)){m.id}}
              td m.subject
              td m.recipients.collect{|r| r.to_s}.join(", ")
              td m.from
              td m.sent
            end
          end
        end
      end
    end
    
    def new_message
      html do
        form :method => 'post', :action => '/messages' do
          h1 "New Message"
          
          dl do
            dt {label 'recipient(s)'} 
            dd {input :type => 'text', :name => 'recipients', :size => 40}
            
            dt {label 'from'}
            dd {input :type => 'text', :name => 'from', :size => 40}
            
            dt {label 'subject'}
            dd {input :type => 'text', :name => 'subject', :size => 40}
            
            dt {label 'body'}
            
            dd {
              @builder.instance_eval { @indent = 0 }
              textarea(:name => 'body', :cols => 40, :rows => 3)
              @builder.instance_eval { @indent = 2 }
            }
          end
          
          h3 "Callback (optional)"
          dl do
            dt {label 'url'} 
            dd {input :type => 'text', :name => 'callback_url', :size => 40}
            
            dt {label 'method'}
            dd {select(:name => 'callback_method'){['get','post','put','delete'].each{|method| option(:value => method){method}}}}
            
            dt {label 'username'}
            dd {input :type => 'text', :name => 'callback_username', :size => 40}
            
            dt {label 'password'}
            dd {input :type => 'password', :name => 'callback_password', :size => 40}
          end
          
          button(:type => 'submit') {"submit"}
        end
      end
    end
  end
  
  default_format :HTML
end